<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="处理器名称" prop="appName">
        <el-input v-model="queryParams.title" placeholder="请输入处理器名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleQuery('reset')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['infra:job:create']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleQuery"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="appName" align="center" prop="appname" />
      <el-table-column label="名称" align="center" prop="title" />
      <el-table-column label="注册方式" align="center">
        <template slot-scope="scope">
          <span> {{scope.row.addressType == 0 ?'自动注册':'手动注册'}} </span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="主机列表" align="center" prop="addressList" />
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          {{parseTime(scope.row.updateTime)}}
        </template>
      </el-table-column>
      <el-table-column label="操作"  class-name="small-padding fixed-width" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['infra:job:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.start + 1" :limit.sync="queryParams.length"
                @pagination="handleQuery"/>

    <el-dialog :visible.sync="show" @close="dataForm = {}" title="添加执行器" width="500px" append-to-body>
      <el-form ref="form" :model="dataForm" label-width="100px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="AppName：">
              <el-input v-model="dataForm.appname" placeholder="请输入AppName" />
            </el-form-item>
            <el-form-item label="名称：">
              <el-input v-model="dataForm.title" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="注册方式：">
              <el-select @change="addressType" v-model="dataForm.addressType" placeholder="请输入注册方式" >
                <el-option label="自动注册" value="0"></el-option>
                <el-option label="手动注册" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="主机列表：">
              <el-input type="textarea" :disabled="dataForm.addressType == 0" :autosize="{ minRows: 4, maxRows: 8 }" v-model="dataForm.addressList" placeholder="请输入主机列表" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addHandlerJob, delHandlerJob, listHandlerJob, updateHandlerJob} from "@/api/infra/job";

export default {
  name: "index",
  data(){
    return {
      show:false,
      dataForm:{},
      total: 0,
      queryParams:{
        start: 0,
        length: 10
      },
      showSearch: true,

      dataList:[],
      loading: false
    }
  },
  created() {
    this.handleQuery();
  },
  methods:{
    addressType(e){
      if (e == 0) this.dataForm.addressList = ''
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.dataForm.id) {
            addHandlerJob(this.dataForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.show = false;
              this.handleQuery();
            });
          } else {
            updateHandlerJob(this.dataForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.show = false;
              this.handleQuery();
            });
          }
        }
      });
    },
    handleAdd(){
      this.show = true
    },
    init(){
      this.queryParams = {
        start: 0,
          length: 10
      }
    },
    handleUpdate(row){
      this.dataForm = row
      this.dataForm.addressType = row.addressType+""
      this.show = true
    },
    handleDelete(row){
      this.$modal.confirm('是否确认删除名称为"' + row.appname + '"的数据项?').then(function() {
        return delHandlerJob(row.id);
      }).then(() => {
        this.handleQuery();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleQuery(type){
      if (type == 'reset') this.queryParams.init()
      this.loading = true
      listHandlerJob(this.queryParams).then(res => {
        this.dataList = res.data
        this.total = res.recordsTotal
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped>
.form {
  padding:20px 40px 20px 0;
  position:absolute;
  top:0px;
  height: 100vh;
  width: 100%;
  background-color:#fff;
  z-index: 1999;
}
</style>
